package org.grottarossa.ui.core.controls.skiptask;


import java.util.ResourceBundle;

import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.base.BasePanelView;

import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

/***
 * Controllo per lo skip di un task
 * @author 
 *
 */
public class SkipTask extends BasePanelView<SkipTaskPresenter> implements ClickListener
{
	private SkipTaskForm m_skipForm;
	private Button m_btnCancel;
	private Button m_btnOk;

	public SkipTask(ApplicationSession appSession, ResourceBundle localizer)
	{
		super(appSession, localizer);
		initializeComponents();
	}

	@Override
	protected void initializeComponents()
	{
		VerticalLayout layout = new VerticalLayout();
		setLayout(layout);
		getLayout().setWidth(100, Sizeable.UNITS_PERCENTAGE);
		getLayout().setHeight(100, Sizeable.UNITS_PERCENTAGE);

		m_skipForm = new SkipTaskForm(m_appSession, m_localizer);
		m_skipForm.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		m_skipForm.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		m_btnOk = new Button(m_localizer.getString("form.buttons.ok"));
		m_btnOk.addListener((ClickListener)this);
		m_btnCancel = new Button(m_localizer.getString("form.buttons.cancel"));
		m_btnCancel.addListener((ClickListener)this);
		layout.addComponent(m_skipForm);

		HorizontalLayout buttonsBar = new HorizontalLayout();
		buttonsBar.setMargin(true, false, false, false);
		buttonsBar.addComponent(m_btnOk);
		buttonsBar.addComponent(m_btnCancel);
		layout.addComponent(buttonsBar);

		layout.setExpandRatio(m_skipForm, 1000);
		layout.setExpandRatio(buttonsBar, 1);
	}

	public void buttonClick(ClickEvent event)
	{
		if(event.getButton() == m_btnOk)
			m_presenter.ok();
		else if(event.getButton() == m_btnCancel)
			m_presenter.cancel();
	}

	/***
	 * Aggiunge un listener
	 * @param listener
	 */
	public void addListener(ISkipTaskListener listener)
	{
		m_presenter.addListener(listener);
	}

	/***
	 * Imposta i dati sulla form di skip
	 * @param data
	 */
	public void setSkipTaskData(SkipTaskData data)
	{
		m_skipForm.setItemDataSource(new BeanItem<SkipTaskData>(data));
	}

}
